from apps.common.base.dao import BaseDao
from apps.user.serializers import *
from django.db.models import F
from apps.system.rank.dao import RankDao
import datetime
from decimal import Decimal

class UserDao(BaseDao):
    MODEL_CLASS = UserModel
    SERI_CLASS = UserBaseSerializer

    def findAllCustom(self):
        return self.findAll().filter(role='custom')

    def addWallet(self, scene, obj, value):
        # print('增加积分', scene, obj, value)
        value = Decimal(value).quantize(Decimal("0.00"))
        if value == 0:
            return
        value = value.quantize(Decimal("0.00"))
        if scene == 'incomes':
            obj = UserModel.objects.get(id=obj.id)
            obj.incomes = F('incomes') + value
            obj.save()
        elif scene == 'incomesLocked':
            obj = UserModel.objects.get(id=obj.id)
            obj.incomesLocked = F('incomesLocked') + value
            obj.save()
        elif scene == 'historyIncomes':
            obj = UserModel.objects.get(id=obj.id)
            obj.historyIncomes = F('historyIncomes') + value
            obj.save()
        elif scene == 'balance':
            obj = UserModel.objects.get(id=obj.id)
            obj.balance = F('balance') + value
            obj.save()
        elif scene == 'contributionValue':
            obj = UserModel.objects.get(id=obj.id)
            obj.contributionValue = F('contributionValue') + value
            obj.save()
        elif scene == 'incomesOriginal':
            obj = UserModel.objects.get(id=obj.id)
            obj.incomesOriginal = F('incomesOriginal') + value
            obj.save()
        elif scene == 'cashSellingMoney':
            obj = UserModel.objects.get(id=obj.id)
            obj.cashSellingMoney = F('cashSellingMoney') + value
            obj.save()
        elif scene == 'shop_balance':
            obj = UserModel.objects.get(id=obj.id)
            obj.shop_balance = F('shop_balance') + value
            obj.save()
        else:
            print('payway不存在')

    def subWallet(self, scene, obj, value):
        print('减少积分', scene, obj, value)
        value = Decimal(value).quantize(Decimal("0.00"))
        if value == 0:
            return
        if scene == 'incomes':
            obj = UserModel.objects.get(id=obj.id)
            obj.incomes = F('incomes') - value
            obj.save()
        elif scene == 'incomesLocked':
            obj = UserModel.objects.get(id=obj.id)
            obj.incomesLocked = F('incomesLocked') - value
            obj.save()
        elif scene == 'historyIncomes':
            obj = UserModel.objects.get(id=obj.id)
            obj.historyIncomes = F('historyIncomes') - value
            obj.save()
        elif scene == 'balance':
            obj = UserModel.objects.get(id=obj.id)
            obj.balance = F('balance') - value
            obj.save()
        elif scene == 'contributionValue':
            obj = UserModel.objects.get(id=obj.id)
            obj.contributionValue = F('contributionValue') - value
            obj.save()
        elif scene == 'incomesOriginal':
            obj = UserModel.objects.get(id=obj.id)
            obj.incomesOriginal = F('incomesOriginal') - value
            obj.save()
        elif scene == 'cashSellingMoney':
            obj = UserModel.objects.get(id=obj.id)
            obj.cashSellingMoney = F('cashSellingMoney') - value
            obj.save()
        elif scene == 'shop_balance':
            obj = UserModel.objects.get(id=obj.id)
            obj.shop_balance = F('shop_balance') - value
            obj.save()
        else:
            print('payway不存在')
            
    def addRank(self, user, grade):
        if user.rank.grade < grade:
            user = UserModel.objects.get(id=user.id)
            user.rank = RankDao().findOneByGrade(grade)
            user.save()

    def checkParentRank(self, user):
        if user.parent is None:
            return True
        # if user.parent.rank.grade == 0: # 粉丝不升级
        #     return True
        limit = 2
        level = 2
        _parent = UserModel.objects.get(id=user.parent.id)
        if UserDao().findAll().filter(parent=_parent, rank__grade__gte=1).count()>=limit:
            if _parent.rank.grade < level:
                _parent.rank = RankDao().findOneByGrade(level)
                _parent.save()
